﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Fanfou
{
    /// <summary>
    /// Display class
    /// </summary>
    public static class Display
    {
        /// <summary>
        /// Display a single empty line
        /// </summary>
        public static void WriteLine()
        {
            Console.WriteLine();
        }

        /// <summary>
        /// Display the single text string in a separate line
        /// </summary>
        /// <param name="text">The text to be displayed</param>
        public static void WriteLine(string text)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(text);
            Console.ResetColor();
        }


        /// <summary>
        /// Display the single text string
        /// </summary>
        /// <param name="text">The text to be displayed</param>
        public static void Write(string text)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(text);
            Console.ResetColor();
        }


        /// <summary>
        /// Display the single text string in a separate line with specific foreground color
        /// </summary>
        /// <param name="text">The text to be displayed</param>
        /// <param name="foreColor">The font color</param>
        public static void WriteLine(string text, ConsoleColor foreColor)
        {
            Console.ForegroundColor = foreColor;
            Console.WriteLine(text);
            Console.ResetColor();
        }


        /// <summary>
        /// Display the single text string with specific foreground color
        /// </summary>
        /// <param name="text">The text to be displayed</param>
        /// <param name="foreColor">The font color</param>
        public static void Write(string text, ConsoleColor foreColor)
        {
            Console.ForegroundColor = foreColor;
            Console.Write(text);
            Console.ResetColor();
        }


        /// <summary>
        /// Display the single text string
        /// </summary>
        /// <param name="text">The text to be displayed</param>
        /// <param name="isContactIncluded">Set to true if "@someone" is included in the message, otherwise set to false</param>
        public static void WriteLine(string text, bool isContactIncluded)
        {
            if (!isContactIncluded)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(text);
                Console.ResetColor();
            }
            else
            {
                List<string> nameList = new List<string>();
                List<string> devidedMessageText = new List<string>();
                List<bool> messageReproSequence = new List<bool>();

                DivideMessageToNameAndMessage(text, out nameList, out devidedMessageText, out messageReproSequence);
                int nameCount = 0;
                int msgCount = 0;
                foreach (bool nameOrMsg in messageReproSequence)
                {

                    if (nameOrMsg)
                    {
                        Display.Write(nameList[nameCount], ConsoleColor.Yellow);
                        nameCount++;
                    }
                    else
                    {
                        Display.Write(devidedMessageText[msgCount], ConsoleColor.Green);
                        msgCount++;
                    }
                }
                Console.WriteLine();
            }
        }


        /// <summary>
        /// To devide a message which includes "@" information.
        /// </summary>
        /// <param name="text">The original message text</param>
        /// <param name="nameList">The @name list</param>
        /// <param name="devidedMessageText">The devided message</param>
        /// <param name="messageReproSequence">The message repro command sequence</param>
        private static void DivideMessageToNameAndMessage(
            string text, 
            out List<string> nameList, 
            out List<string> devidedMessageText, 
            out List<bool> messageReproSequence)
        {
            nameList = new List<string>();
            devidedMessageText = new List<string>();
            messageReproSequence = new List<bool>();

            const string AT_SYMBOL = "@";
            const string SPACE_SYMBOL = " ";

            while (text.IndexOf(AT_SYMBOL) > -1)
            {
                if (text.IndexOf(AT_SYMBOL) == 0)
                {
                    try
                    {
                        nameList.Add(text.Substring(0, text.IndexOf(SPACE_SYMBOL)));
                    }
                    catch
                    {
                        nameList.Add(text);
                    }
                    // Add true means output name
                    messageReproSequence.Add(true);
                    if (text.IndexOf(SPACE_SYMBOL) > -1)
                    {
                        text = text.Substring(text.IndexOf(SPACE_SYMBOL));
                    }
                    else
                    {
                        break;
                    }
                    if (text.IndexOf(AT_SYMBOL) < 0)
                    {
                        devidedMessageText.Add(text);
                        // Add false means output message
                        messageReproSequence.Add(false);
                        break;
                    }
                }
                else
                {
                    devidedMessageText.Add(text.Substring(0, text.IndexOf(AT_SYMBOL)));
                    text = text.Substring(text.IndexOf(AT_SYMBOL), text.Length - text.IndexOf(AT_SYMBOL));
                    // Add false means output message
                    messageReproSequence.Add(false);
                }
            }
        }
    }
}
